diff --git a/class-RegisterCSS.php b/class-RegisterCSS.php index ae3f5ad..6426ace 100644 --- a/class-RegisterCSS.php +++ b/class-RegisterCSS.php @@ -1,92 +1,95 @@ . class RegisterCSS { /** * Stylesheets */ private $css = []; /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Register a new style name * * @param string $name CSS name, like: "jquery-ui". * @param string $url CSS url, like "http://example.org/styles/jquery-ui.css". */ public function register( $uid, $url ) { $this->css[ $uid ] = new CSS( $url ); } /** - * Enqueue a previous registered CSS name + * Enqueue a previously registered CSS name * - * @param string $uid CSS name. + * @param string $uid CSS name + * @param boolean $report_errors In case of errors, report the error? */ - public function enqueue( $uid ) { + public function enqueue( $uid, $report_errors = true ) { if( empty( $this->css[ $uid ] ) ) { - error( "unknown stylesheet $uid" ); + if( $report_errors ) { + error( "unknown stylesheet $uid" ); + } } else { $this->css[ $uid ]->enqueue = true; } } /** * Enqueue all registered style that are not registered as private use */ public function printAll( $force = false ) { foreach( $this->css as $uid => $css ) { if( $css->enqueue && !$css->enqueued ) { self::link( $css->url, $uid ); $this->css[ $uid ]->enqueued = true; } } } /** * Print the CSS link tag */ public static function link( $url, $uid ) { if( CACHE_BUSTER ) { $url .= false === strpos( $url, '?' ) ? '?' : '&'; $url .= CACHE_BUSTER; } echo "\n\t"; } } class CSS { public $url; public $enqueue = false; public $enqueued = false; public function __construct( $url ) { $this->url = $url; } } diff --git a/class-RegisterJS.php b/class-RegisterJS.php index bd3b0f6..e8c2420 100644 --- a/class-RegisterJS.php +++ b/class-RegisterJS.php @@ -1,178 +1,185 @@ . /** * Register and enqueue JS libraries */ class RegisterJS { /** * Default position */ public static $DEFAULT = 'header'; /** * Script names */ private $js = []; /** * Get the singleton instance * * @return self */ public static function instance() { static $me = false; if( ! $me ) { $me = new self(); } return $me; } /** * Register a new script name * * @param string $name Script name, like: "jquery" * @param mixed $url Script url, like "http://example.org/lib/jquery.js" * @param string $position header|footer * @param array $dependencies Dependent script names */ public function register( $uid, $url, $position = null, $dependencies = [] ) { if( ! $position ) { $position = self::$DEFAULT; } $this->js[ $uid ] = new JS( $uid, $url, $position, $dependencies ); } /** * Register an inline script * * @param $name string * @param $data string * @param $position before|after */ public function registerInline( $uid, $data, $position ) { $this->js[ $uid ]->inline[ $position ][] = $data; } /** * Enqueue a previous registered JS name * - * @param $name string JS name + * Note that if there is a stylesheet with the same UID, it will be + * enqueued as well. + * + * @param $name string JS name * @param $position string Place it in the head of the page or not */ public function enqueue( $uid, $position = null ) { $js = $this->js[ $uid ]; // eventually override script position if( $position ) { $js->position = $position; } // eventually mark to be enqueued with also its dependencies if( !$js->enqueue ) { + + // also enqueue the stylesheet with the same UID (if it exists) + RegisterCSS::instance()->enqueue( $uid, false ); + $js->enqueue = true; foreach( $js->dependencies as $dependency_uid ) { $this->enqueue( $dependency_uid ); } } } /** * Print all the JS from a specified position * * @param $position string */ public function printAll( $position ) { $glue = $position === 'header' ? "\n\t" : "\n"; foreach( $this->js as $js ) { if( $js->enqueue && $js->position === $position ) { $js->printInline( 'before', $glue ); $js->printNormal( $glue ); $js->printInline( 'after', $glue ); } } } } class JS { public $uid; public $url; public $position; public $enqueue = false; public $inline; public $dependencies; /** * Construct * * @param $uid string * @param $url string * @param $position string header|footer */ public function __construct( $uid, $url, $position, $dependencies ) { $this->uid = $uid; $this->url = $url; $this->position = $position; $this->dependencies = $dependencies; $this->inline = [ 'after' => [], 'before' => [], ]; } /** * Print inline JavaScript parts * * @param $position before|after * @param $glue string */ public function printInline( $position, $glue ) { $parts = $this->inline[ $position ]; if( $parts ) { echo "$glue"; if( DEBUG ) { echo ""; } } } /** * Print the normal JS inclusion * * @param $glue string */ public function printNormal( $glue ) { $url = $this->url; if( CACHE_BUSTER ) { $url .= false === strpos( $url, '?' ) ? '?' : '&'; $url .= CACHE_BUSTER; } echo "$glue"; if( DEBUG ) { echo ""; } } } diff --git a/functions.php b/functions.php index 4cbdd43..628d339 100644 --- a/functions.php +++ b/functions.php @@ -1,1120 +1,1122 @@ . /** * Check if the user do not want to be tracked * * See the HTTP 'DNT' header. * * @return boolean */ function do_not_track() { return isset( $_SERVER['HTTP_DNT'] ) && $_SERVER['HTTP_DNT'] === '1'; } /* * Configuration helpers * * Shortcuts useful when you declare a configuration file. */ /** * Define a constant if it does not exist * * @param string $name Constant name * @param string $value Constant value */ function define_default( $name, $value ) { defined( $name ) or define( $name, $value ); } /* * Template helpers * * Shortcuts useful when you build a page */ /** * Return a ' selected="selected"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function selected( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'selected', 'selected', $helper, $current, $force); } /** * Return a ' checked="checked"' HTML attribute under some circumstances * * This is useful for the HTML tag. * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function checked( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'checked', 'checked', $helper, $current, $force); } /** * Return a ' disabled="disabled"' HTML attribute under some circumstances * * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function disabled( $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { return html_attribute_when_matching( 'disabled', 'disabled', $helper, $current, $force ); } /** * Return an HTML attribute under some circumstances * * @param string $attribute HTML attribute name e.g. 'disabled' * @param string $value HTML attribute value e.g. * @param mixed $helper If this is the only arg, return * @param mixed $current If this matches $helper, return * @param bool $force If this is true, return */ function html_attribute_when_matching( $attribute, $value, $helper = PHP_INT_MAX, $current = PHP_INT_MAX, $force = false ) { if( $helper === $current || $helper && PHP_INT_MAX === $current || $force ) { return HTML::property( $attribute, $value ); } return ''; } /** * Get an HTML attribute ' value="$v"' * * The value will be sanitized. * * @param string $v * @return string */ function value( $v ) { return HTML::property( 'value', $v ); } /** * Shortcut for htmlentities() * * Return an HTML-sanitized untrusted string to be safe from XSS. * * @param string $s e.g. 'Hello